﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.Model;
using OgilvyOne.BLL;

public partial class admin_column_column_details : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string pid = Request.Params["parentid"];
            
            if (pid != null && pid != "")
            {
                //栏目类型/模板列表初始
                TypeManageBll tmb = new TypeManageBll();
                DataSet data = tmb.GetList("");
                for (int i = 0; i < data.Tables.Count; i++)
                {
                    DataTable table = data.Tables[i];
                    for (int j = 0; j < table.Rows.Count; j++)
                    {
                        DataRow row = table.Rows[j];

                        this.columntypeid.Items.Add(new ListItem(row["CTNAME"].ToString(), row["COLUMNTYPEID"].ToString()));
                    }
                }
                ModelManageBLL mmb = new ModelManageBLL();
                DataSet temdata = mmb.GetModel("");
                for (int i = 0; i < temdata.Tables.Count; i++)
                {
                    DataTable table = temdata.Tables[i];
                    for (int j = 0; j < table.Rows.Count; j++)
                    {
                        DataRow row = table.Rows[j];
                        
                        this.templatedetailid.Items.Add(new ListItem(row["tem_name"].ToString(), row["tem_id"].ToString()));
                        this.templateindexid.Items.Add(new ListItem(row["tem_name"].ToString(), row["tem_id"].ToString()));
                    }
                }
                //---end
                ColumnBll cb = new ColumnBll();

                //初始模板标签
                IList htmltaglist = cb.getHtmlTempLabelList();
                for (int i = 0; i < htmltaglist.Count; i++)
                {
                    TemplateLabelModel tlm = (TemplateLabelModel)htmltaglist[i];
                    this.htmlTags.Items.Add(new ListItem(tlm.Name, tlm.Id.ToString()));
                }

                this.parentid.Value = pid;
                string id = Request.Params["columnid"];
                
                this.cnavi.Text = cb.LayeridColumnNaiv(Layerid);
                if (id != null && id != "")
                {
                    this.classid.Value = id;

                    ColumnModel cm = cb.GetColumn(int.Parse(id));
                    this.classname.Text = cm.Classname;
                    this.classpic.Value = cm.Classpic;

                    if (cm.Classpic != null && cm.Classpic != "")
                    {
                        this.classpicimg.Src = path + "/uploadfiles/image/" + cm.Classpic;
                    }
                    else
                    {
                        this.classpicimg.Visible = false;
                    }

                    this.cname.Text = "修改";
                    this.columndesc.Text = cm.Columndesc;
                    this.columnfilename.Text = cm.Columnfilename;
                    this.columnkeyword.Text = cm.Columnkeyword;
                    this.columntitle.Text = cm.Columntitle;
                    this.columntypeid.Text = cm.Columntype.ToString();
                    this.detail.Text = cm.Detail;
                    this.eclassname.Text = cm.Eclassname;
                    this.isauditing.Text = cm.Isauditing.ToString();
                    this.isshow.Text = cm.Isshow.ToString();

                    this.orderid.Text = cm.Orderid.ToString();
                    this.templatedetailid.Text = cm.Templatedetail.ToString();
                    this.templateindexid.Text = cm.Templateindex.ToString();
                    this.isstaticpub.Text = cm.Isstaticpub.ToString();

                    this.orderid.Items.Add(new ListItem(cm.Orderid.ToString(), cm.Orderid.ToString()));
                    this.orderid.Enabled = false;
                    if (cm.Is_class_index == 1)
                    {
                        this.is_class_index.Checked = true;
                    }
                    if (cm.Is_index == 1)
                    {
                        this.is_index.Checked = true;
                    }
                    this.class_is_static.Text = cm.Class_is_static.ToString();
                    //this.htmlTags.Text = cm.Html_tags;
                    if (cm.Html_tags != null)
                    {
                        string[] hts = cm.Html_tags.Split(',');
                        foreach (string str in hts)
                        {
                            for (int i = 0; i < this.htmlTags.Items.Count; i++)
                            {
                                if (this.htmlTags.Items[i].Value == str)
                                {
                                    this.htmlTags.Items[i].Selected = true;
                                }
                            }
                        } 
                    }
                }
                else
                {
                    int maxorderid = cb.MaxOrder(int.Parse(pid));
                    this.orderid.Items.Add(new ListItem((maxorderid + 1).ToString(), (maxorderid + 1).ToString()));
                    this.classpicimg.Visible = false;
                }

                
            }
            else
            {//错误调用

            }
        }
    }

    protected void subBut_click(object sender, EventArgs e)
    {
        string classid = this.classid.Value;

        ColumnModel cm = new ColumnModel();
        cm.Classname = this.classname.Text ;
        cm.Classpic = this.classpic.Value;
        cm.Parentid = int.Parse(this.parentid.Value);
        cm.Columndesc = this.columndesc.Text;
        cm.Columnfilename = this.columnfilename.Text ;
        cm.Columnkeyword = this.columnkeyword.Text;
        cm.Columntitle = this.columntitle.Text;
        cm.Columntype = int.Parse(this.columntypeid.Text);
        cm.Detail = this.detail.Text;
        cm.Eclassname = this.eclassname.Text;
        cm.Isauditing = int.Parse(this.isauditing.Text);
        cm.Isshow = int.Parse(this.isshow.Text);


        cm.Templatedetail = int.Parse(this.templatedetailid.Text);
        cm.Templateindex = int.Parse(this.templateindexid.Text);
        cm.Isstaticpub = int.Parse(this.isstaticpub.Text);
        if(this.is_class_index.Checked ==true){
            cm.Is_class_index = 1;
        }
        if (this.is_index.Checked == true)
        {
            cm.Is_index = 1;
        }
        cm.Class_is_static = int.Parse(this.class_is_static.Text);
        string htags = "";
        foreach (ListItem li in htmlTags.Items)
        {
            if (li.Selected == true)
            {
                htags += li.Value + ",";
            }
        }
        cm.Html_tags = htags;

        ColumnBll cb = new ColumnBll();
        bool su = false;
        if (classid != null && classid != "")
        {//修改
            PopedomValidate(ColumnBll.COLUMN_LAYERID, MOD);

            cm.Classid = int.Parse(classid);
            su = cb.ModColumn(cm);
            if (su)
            {
                this.insertLog("修改栏目:" + cm.Classname);
            }
        }
        else
        {//新增
            PopedomValidate(ColumnBll.COLUMN_LAYERID, ADD);
            cm.Orderid = int.Parse(this.orderid.Text);
            su = cb.AddColumn(cm);
            if (su)
            {
                this.insertLog("新增栏目:" + cm.Classname);
            }
        }

        if (su == true)
        {//保存成功
            Response.Redirect("column_list.aspx?s=1&columnid=" + this.parentid.Value + "&layerid=" + Request.Params["layerid"], true);
        }
        else
        {//保存失败
            this.message.Text = "<script language='javascript'>alert('保存失败!')</script>";
        }
    }
    
}
